import unittest

from util.sql_builder import SqlBuilder


class SqlBuilderTestCase(unittest.TestCase):
    def test_build_without_sql(self):
        sql_builder = SqlBuilder('SELECT * FROM person p')
        self.assertEqual('SELECT * FROM person p;', sql_builder.build())

    def test_build_with_in_sql(self):
        sql_builder = SqlBuilder('SELECT * FROM person p')
        sql_builder.append_and_sql('p.name=marvin')
        sql_builder.append_or_sql('p.name=marvin2')
        self.assertEqual('SELECT * FROM person p WHERE p.name=marvin OR p.name=marvin2;', sql_builder.build())

    def test_build_with_in_criteria(self):
        sql_builder = SqlBuilder('SELECT * FROM person p')
        sql_builder.append_and_sql('p.name=marvin')
        sql_builder.append_or_sql('p.name=marvin2')
        sql_builder.append_or_in_criteria('p.address', ['zhuhai', 'dongguan'])
        self.assertEqual(
            "SELECT * FROM person p WHERE p.name=marvin OR p.name=marvin2 OR p.address IN ('zhuhai','dongguan');",
            sql_builder.build())

    def test_print_something(self):
        sql_builder = SqlBuilder('SELECT p.address,r.city FROM person p LEFT JOIN address a ON p.id = a.person_id')
        sql_builder.append_and_sql('p.name = marvin')
        sql_builder.append_and_in_criteria('a.city', ['zhuhai', 'dongguan'])
        print(sql_builder.build_with_paging(5, 10))
        self.assertTrue(True)


if __name__ == '__main__':
    unittest.main()
